#include "ScoreSystem.h"
#include <fstream>
#include <algorithm>

LeaderBoard::LeaderBoard(void)
{
	P1_Score = P2_Score = 0;
	fstream scorefile("highscore");
	string tempstring;
	int tempint;
	while(!scorefile.eof() && ScoreList.size() < 10)
	{
		scorefile >> tempstring;
		scorefile >> tempint;
		ScoreList.push_back(pair<string, int>(tempstring, tempint));
	}
	scorefile.close();
}

bool sortfunc(pair<string, int> i, pair<string, int> j)
{
	return i.second < j.second;
}

void LeaderBoard::SaveFile(void)
{
	ScoreList.sort(sortfunc);
	list<pair<string, int>>::iterator i;	//iterator for list

	//File Input
	fstream scorefile("highscore", ios::out | ios::trunc);
	for(i = ScoreList.begin(); i != ScoreList.end(); ++i)
	{
		scorefile << i->first << endl;
		scorefile << i->second << endl;
	}

	scorefile.close();
}